#   Copyright (c) 2008 ToI-Planning, All rights reserved.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#  $Id: App.pm 10 2009-10-22 23:47:27Z usualoma $

package ScriptInjection::App;
use strict;

sub source_edit_entry {
	my ($cb, $app, $tmpl) = @_;
	my $plugin = MT->component('ScriptInjection');
	my $blog_id = $app->param('blog_id') or return;

	my $library_script = $plugin->get_config_value(
        'injection_' . $app->param('_type') . '_library_script',
		'blog:' . $blog_id
	);

	my $library_file = $plugin->get_config_value(
        'injection_' . $app->param('_type') . '_library_file',
		'blog:' . $blog_id
	);

	if ($library_file && -f $library_file) {
		$library_script .=
			do{ open(my $fh, '<', $library_file); local $/; <$fh> };
	}

	my $script = $plugin->get_config_value(
        'injection_' . $app->param('_type') . '_script',
		'blog:' . $blog_id
	);

	my $file = $plugin->get_config_value(
        'injection_' . $app->param('_type') . '_file',
		'blog:' . $blog_id
	);

	if ($file && -f $file) {
		$script .= do{ open(my $fh, '<', $file); local $/; <$fh> };
	}

	my @fields = ();
	eval {
		require CustomFields::Field;
	};
	if (! $@) {
		@fields = CustomFields::Field->load({
			'blog_id' => [ 0, $blog_id ],
			'obj_type' => $app->param('_type'),
		});
	}
	my $hash = '{}';
	if (@fields) {
		require MT::Util;
		$hash = "{" . join(",", map({
			"'customfield_" . $_->basename . "': ['" . $_->tag . "', '" .
			MT::Util::encode_js($_->name) . "']";
		} @fields)) . "}";
	}

	if ($script) {
		my $insert = <<__EOH__;
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
(function() {
	var inputs = {};
	(function() {
		var l = [
			'title', 'tags', 'excerpt', 'keywords-field'
		];
		for (var i = 0; i < l.length; i++) {
			inputs[l[i]] = document.getElementById(l[i]);
		}
		var h = $hash;
		for (var k in h) {
			var l = h[k];
			for (var i = 0; i < l.length; i++) {
				if (i == 0) {
					inputs[l[i]] = document.getElementById(k);
					if (! inputs[l[i]]) {
						inputs[l[i]] = {
							'date': document.getElementById('d_' + k),
							'time': document.getElementById('t_' + k)
						};
					}
				}
				else {
					inputs[l[i]] = inputs[l[i-1]];
				}
			}
		}
	})();
$library_script
	google.setOnLoadCallback(function() {
$script
	});
})();
</script>
__EOH__

		$$tmpl =~ s{(<mt:include\s*name="include/footer.tmpl")}{$insert\n$1};
	}
}

1;
